/*
 * 
 *
 * 
 */
package it.luigiarpino.alt2jsim.netpanel.transit;

import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Rectangle;
import java.lang.Math;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;

/**
 *
 * @author  Luigi Lorenzo Arpino 534/559
 */
public class TreePanel extends JPanel {
    
    /**
	 * 
	 */
	private static final long serialVersionUID = 2247423241370834455L;
	private String[] nomiNodi;
    private BufferedImage bufferedImageNodoTransit;
    private BufferedImage bufferedImageReteStub;

    /**Visualizza un nodo transit e le relative reti stub
     Nota: il primo elemento del vettore nomiNodi
     deve essere il nodo transit*/
    public TreePanel(String[] nomiNodi_) {
        super();
        try { 
            javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());     
    	} 
    	catch(Exception e) { }
        this.nomiNodi=nomiNodi_;
        try{
            // carica le gif 
            java.net.URL u = this.getClass().getResource("/it/luigiarpino/alt2jsim/images/transitNodeBlu.gif");
            this.bufferedImageNodoTransit= ImageIO.read(u);
            u = this.getClass().getResource("/it/luigiarpino/alt2jsim/images/reteStub.gif");
            this.bufferedImageReteStub= ImageIO.read(u);
        }
        catch(java.lang.Exception e){
        }
    }
        
    
     public void paintComponent(Graphics g) {
       Rectangle r=new Rectangle(this.getBounds());
       g.clearRect(0,0,r.width ,r.height);
       g.setColor(Color.BLACK);
       int numNode=this.nomiNodi.length;
       //int centroX= (r.x+r.width)/2;
       //int centroY=(r.y+r.height)/2;
       //Disegno i link
       g.setColor(Color.BLACK);
       double angolo2=4.71;
       int x2=(int)(Math.cos(angolo2)*(r.width*0.35)+(r.width/2));
       int y2=(int)(Math.sin(angolo2)*(r.height*0.35)+(r.height/2));
       for(int i=0;i<numNode;i++){ 
           double angolo1=(6.28/numNode)*i+4.71;
           int x1=(int)(Math.cos(angolo1)*(r.width*0.35)+(r.width/2));
           int y1=(int)(Math.sin(angolo1)*(r.height*0.35)+(r.height/2));
           g.drawLine(x1,y1,x2,y2);
       }
       //Disegno le reti stub
       for (int i=1;i<numNode;i++){
           double angolo=(6.28/numNode)*i+4.71;
           int x=(int)(Math.cos(angolo)*(r.width*0.35)+(r.width/2));
           int y=(int)(Math.sin(angolo)*(r.height*0.35)+(r.height/2));
           try{
               Graphics2D g2 = (Graphics2D) g;
               g2.drawImage(this.bufferedImageReteStub,null,x-18,y-13 );
           }
           catch(java.lang.Exception e){
               int raggio=5;
               g.fillOval(x-raggio, y-raggio, 2*raggio,2*raggio);
           }
       }
       //Disegno il nodo transit
       int x=(int)(Math.cos(4.71)*(r.width*0.35)+(r.width/2));
       int y=(int)(Math.sin(4.71)*(r.height*0.35)+(r.height/2));
       try{
           Graphics2D g2 = (Graphics2D) g;
           g2.drawImage(this.bufferedImageNodoTransit,null,x-18,y-13 );
       }
       catch(java.lang.Exception e){
           g.setColor(Color.RED);
           int raggio=5;
           g.fillOval(x-raggio, y-raggio, 2*raggio,2*raggio);
       }
       //Nomino i nodi
       for (int i=0;i<numNode;i++){
           double angolo=(6.28/numNode)*i+4.71;
           x=(int)(Math.cos(angolo)*(r.width*0.42)+(r.width/2));
           y=(int)(Math.sin(angolo)*(r.height*0.42)+(r.height/2));
           g.drawString(this.nomiNodi[i], x, y);
       }     
  }//fine paintComponent()
     
}//fine classe

